﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
namespace WordDowngrade
{
    public static class Downgrader
    {

        public static void Downgrade(object filePath,object newFileName)
        {
            object missing = System.Reflection.Missing.Value;
            String f = (string)filePath;
            Microsoft.Office.Interop.Word.ApplicationClass wordApp= new ApplicationClass();
            Microsoft.Office.Interop.Word.Document aDoc = null;
            object readOnly = false;
            object isVisible =true;
            aDoc = wordApp.Documents.Open(ref filePath, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
            aDoc.Activate();
            //aDoc.DowngradeDocument();
            aDoc.Save();
            object d = newFileName;
            object format =Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97;
            aDoc.SaveAs(ref d, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            aDoc.Close(ref missing, ref missing, ref missing);

        }

        public static void searcher(CategoryDict dictionary,CategoryDict ansDict, object filePath)
        {
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
            Microsoft.Office.Interop.Word.Document aDoc = null;
            object readOnly = false;
            object isVisible = true;
            aDoc = wordApp.Documents.Open(ref filePath, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
            aDoc.Activate();
            wordApp.Selection.WholeStory();
            wordApp.Selection.Find.ClearFormatting();
            String[] cats = dictionary.Categories;
            for (int i = 0; i < cats.Length; i++)
            {
                String []lables = dictionary.getLables(cats[i]);
                for (int j = 0; j < lables.Length; j++)
                {
                    object findText = lables[j];
                    bool ans = wordApp.Selection.Find.Execute(ref findText,
                             ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing);
                   wordApp.Selection.Find.ClearFormatting();
                   wordApp.Selection.WholeStory();
                    if (ans)
                    {
                        ansDict.addLabelToCat(cats[i],(String) filePath);
                    }
                }
            }
            aDoc.Close(ref missing, ref missing, ref missing);


        }
    }
}
